package com.ljb.android.component.user.router

import android.app.Activity
import android.content.Context
import android.content.Intent
import android.text.TextUtils
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPStaticUtils
import com.ljb.android.comm.router.core.RouterManager
import com.ljb.android.component.user.bean.UserBean
import com.ljb.android.component.user.common.UserConstant
import com.ljb.android.component.user.view.act.UserLoginActivity
import net.ljb.kt.utils.JsonParser

@Route(path = UserRouter.Service.USER)
class UserRouterServiceImpl : IUserRouterService {

    override fun init(context: Context) {
        //初始化工作，服务注入时会调用
    }

    override fun getUserInfo(): UserBean? {
        val userJson = SPStaticUtils.getString(UserConstant.SPKey.KEY_LOGIN_USER, "")
        if (TextUtils.isEmpty(userJson)) return null
        return JsonParser.fromJsonObj(userJson, UserBean::class.java)
    }

    override fun isLogin(act: Activity?, needLogin: Boolean): Boolean {
        val userInfo = getUserInfo()
        return if (userInfo == null || TextUtils.isEmpty(userInfo.id)) {
            if (needLogin) {
                RouterManager.goActivity(act, UserRouter.Activity.USER_LOGIN)
            }
            false
        } else {
            true
        }
    }

    override fun isLoginForResult(act: Activity, reqCode: Int): Boolean {
        val userInfo = getUserInfo()
        return if (userInfo == null || TextUtils.isEmpty(userInfo.id)) {
            RouterManager.goActivityForResult(act, UserRouter.Activity.USER_LOGIN, null, reqCode)
            val intent = Intent(act, UserLoginActivity::class.java)
            act.startActivityForResult(intent, reqCode)
            false
        } else {
            true
        }
    }


}